package com.sf.dlb.service;

import com.sf.dlb.bean.BzyNewsDataBean;
import com.sf.dlb.constant.ResponseConstant;
import com.sf.dlb.jpa.model.*;
import com.sf.dlb.jpa.repository.*;
import com.sf.dlb.mongo.model.*;
import com.sf.dlb.mongo.repository.NewInfoRepository;
import com.sf.dlb.mongo.repository.NewListRepository;
import com.sf.dlb.utils.*;
import com.sf.dlb.vo.ResultVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.*;

/**
 * Created by jing.tan on 2018/5/21.
 */
@Service
public class NewListService {
    private static Logger logger = LoggerFactory.getLogger(NewListService.class);

    @Autowired
    private BzyExportRecodRepository bzyExportRecodRepository;
    @Autowired
    private NewClassRepository newClassRepository;
    @Autowired
    private BzyOauthService bzyOauthService;
    @Autowired
    private NewListRepository newListRepository;
    @Autowired
    private NewInfoRepository newInfoRepository;
    @Autowired
    private BzyExportRecodService bzyExportRecodService;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Resource
    private Global global;
    /**
     * 获取所有版块新闻列表
     * @return
     */
    public ResultVO getNewList(){
        logger.info("getNewList start ");
        //1 八爪鱼API授权；
        ResultVO resultVO=bzyOauthService.getOauth();
        if(resultVO.getCode()!=200){
            logger.info("getNewList getOauth is null");
            return ResultVOUtil.error(ResponseConstant.ERROR,"getNewList getOauth is null");
        }
        BzyOauthInfo bzyOauthInfo=(BzyOauthInfo)resultVO.getData();
        //删除半个月之前的视频
        deleteBeforeNewList();
        //2 获取任务
        List<BzyExportRecodInfo> bzyExportRecodInfos= bzyExportRecodRepository.findByUsernameAndRemarksAndDelFlag(global.bzy_username,"0","0");
        if(CollectionUtils.isEmpty(bzyExportRecodInfos)){
            logger.info("getNewList bzyExportRecodInfo is null");
            return ResultVOUtil.error(ResponseConstant.ERROR,"getNewList bzyExportRecodInfo is null");
        }
        for (BzyExportRecodInfo bzyExportRecodInfo1:bzyExportRecodInfos) {
            handlerExportData(bzyOauthInfo,bzyExportRecodInfo1);

        }
        logger.info("getNewList end ");
        return ResultVOUtil.success();
    }
    /**
     * 获取指定任务名称新闻列表
     * @param taskId
     * @return
     */
    public ResultVO getNewList(String taskId){
        logger.info("getNewList start taskId={}",taskId);
        //1 八爪鱼API授权；
        ResultVO resultVO=bzyOauthService.getOauth();
        if(resultVO.getCode()!=200){
            logger.info("getNewList getOauth is null");
            return ResultVOUtil.error(ResponseConstant.ERROR,"getNewList getOauth is null");
        }
        BzyOauthInfo bzyOauthInfo=(BzyOauthInfo)resultVO.getData();
        //删除半个月之前的视频
        deleteBeforeNewList();
        //2 获取任务组，任务；
        BzyExportRecodInfo bzyExportRecodInfo= bzyExportRecodRepository.findByTaskIdAndDelFlag(taskId,"0");
        if(null==bzyExportRecodInfo){
            logger.info("getNewList bzyExportRecodInfo is null");
            return ResultVOUtil.error(ResponseConstant.ERROR,"getNewList bzyExportRecodInfo is null");
        }
        handlerExportData(bzyOauthInfo,bzyExportRecodInfo);
        return ResultVOUtil.success();
    }

    /**
     * 处理获取八爪鱼数据
     * @param bzyOauthInfo
     * @param bzyExportRecodInfo
     */
    public void handlerExportData(BzyOauthInfo bzyOauthInfo,BzyExportRecodInfo bzyExportRecodInfo){
        NewGetRecodInfo newGetRecodInfo=new NewGetRecodInfo();
        newGetRecodInfo.setStartTime(new Date());
        newGetRecodInfo.setTaskId(bzyExportRecodInfo.getTaskId());
        //3 根据任务id，偏移量 获取数据；
//        BzyExportDataBean.DataBean exportDataBean=getExportData(bzyOauthInfo,bzyExportRecodInfo);
        String responseBody=bzyExportRecodService.getExportData(bzyOauthInfo,bzyExportRecodInfo);
        BzyNewsDataBean exportDataBean= (BzyNewsDataBean) GsonUtils.getInstance().fromJson(responseBody, BzyNewsDataBean.class);

        newGetRecodInfo.setOffset(bzyExportRecodInfo.getOffset());
        newGetRecodInfo.setSize(bzyExportRecodInfo.getSize());
        if(null==exportDataBean||null==exportDataBean.getData()||CollectionUtils.isEmpty(exportDataBean.getData().getDataList())){
            logger.info("getNewList null==exportDataBean ");
            bzyExportRecodService.saveGetRecodrs(newGetRecodInfo);
            return;
        }
        List<BzyNewsDataBean.DataBean.DataListBean> exportDataBeans=exportDataBean.getData().getDataList();
        newGetRecodInfo.setGetCount(exportDataBeans.size());
        //3.1 判断数据完整性
        List<BzyNewsDataBean.DataBean.DataListBean> checkDataBeansRepeat=checkExportData(exportDataBeans);
        newGetRecodInfo.setFilterCount(newGetRecodInfo.getGetCount()-checkDataBeansRepeat.size());
        //4 去重判断
        List<BzyNewsDataBean.DataBean.DataListBean> exportDataBeansRepeat=repeatExportData(checkDataBeansRepeat);
        newGetRecodInfo.setRepeatCount((newGetRecodInfo.getGetCount()-newGetRecodInfo.getFilterCount())-exportDataBeansRepeat.size());
        //5 正文，列表分开存储；
        saveExportData(exportDataBeansRepeat,bzyExportRecodInfo.getTaskName());
        //6 更新下次导数据偏移量；
        bzyExportRecodService.updateExportDataOffset(bzyExportRecodInfo,exportDataBean.getData().getOffset(),exportDataBean.getData().getTotal(),exportDataBean.getData().getRestTotal());
        //7 每次获取数据记录
        bzyExportRecodService.saveGetRecodrs(newGetRecodInfo);
    }
    /**
     * 删除半个月之前的新闻
     */
    public void deleteBeforeNewList(){
        //查询半个月之前的视频
        Date currentDate15=DateUtils.addSeconds(new Date(),-global.delete_before_news_time);
        Query query =Query.query(Criteria.where("createDate").lt(currentDate15));
        mongoTemplate.remove(query,NewInfo.class);
        mongoTemplate.remove(query,NewList.class);
    }
    /**
     * 保存新闻内容
     * @param exportDataBeansRepeat
     */
    public void saveExportData(List<BzyNewsDataBean.DataBean.DataListBean> exportDataBeansRepeat, String taskName)
    {
        //查询出分类，关联到新闻列表；
        List<NewClassInfo> cmsNewClassInfos= newClassRepository.findAll();
        for (BzyNewsDataBean.DataBean.DataListBean dataListBean:exportDataBeansRepeat) {
            //新闻正文保存-base64编码
            NewInfo cmsNewInfo=new NewInfo();
            cmsNewInfo.setContent(Base64Utils.encodeToString(dataListBean.getContent().getBytes()));
            cmsNewInfo.setState("0");
            cmsNewInfo.setCreateDate(new Date());
            cmsNewInfo.setUpdateDate(new Date());
            cmsNewInfo= newInfoRepository.save(cmsNewInfo);

            NewList newListEntity=new NewList();
            BeanUtils.copyProperties(dataListBean,newListEntity);
            String label=MyStringUtils.stringTrim(dataListBean.getLabel());//label有特殊字符
            newListEntity.setLabel(Base64Utils.encodeToString(label.getBytes()));//标题有特殊字符,如：马蜂窝
            newListEntity.setTitle(Base64Utils.encodeToString(newListEntity.getTitle().getBytes()));//标题有特殊字符,如：马蜂窝
            String source=MyStringUtils.stringTrim(dataListBean.getSource());//source有特殊字符,如：马蜂窝
            newListEntity.setSource(Base64Utils.encodeToString(source.getBytes()));
            newListEntity.setTime(dataListBean.getTime());
            newListEntity.setClassId(getNewClassId(cmsNewClassInfos,taskName));//关联类别
            newListEntity.setNewInfoId(cmsNewInfo.getId()+"");   //更新列表-正文的关联
            //计算正文字数，阅读时长，dlb领取时长，有效阅读区间，有效阅读最大区间，
            String numberStr= MyStringUtils.fileHtmlToChinese(dataListBean.getContent());
            int numberlength=StringUtils.isBlank(numberStr)?0:numberStr.length();
            int readTime=countDlbTime(numberlength);
            int countDlbTime=countDlbTime(numberlength);
            newListEntity.setNumber(numberlength);
            newListEntity.setReadTime(readTime);
            newListEntity.setGetDlbTime(countDlbTime);
            newListEntity.setValidReadInterval(readTime+","+countDlbTime);
            newListEntity.setState("0");
            newListEntity.setCreateDate(new Date());
            newListEntity.setUpdateDate(new Date());
            newListRepository.save(newListEntity);
        }
    }
    //计算dlb领取时长
    public int countDlbTime(int number){
        int dlbTime=global.new_read_default_getdlb_time_long;//默认5s
        //阅读时长大于默认值,计算实际的值
        if(number>dlbTime*global.new_read_second_word_number){
            dlbTime=number/global.new_read_second_word_number;
        }
        return dlbTime;
    }
    /**
     * 处理获取的时间类型；
     */
    public Date handlerRequestDate(BzyNewsDataBean.DataBean.DataListBean dataListBean){
        Date time=null;
        String timeStr=dataListBean.getTime();
        try {
            if(StringUtils.isNotBlank(timeStr)&&timeStr.trim().length()==16){
                if(timeStr.contains("/")){
                    time=DateUtils.parseDate(timeStr,"yyyy/MM/dd HH:mm");
                }else if(timeStr.contains("-")){
                    time=DateUtils.parseDate(timeStr,"yyyy-MM-dd HH:mm");
                }
            }else if(StringUtils.isNotBlank(timeStr)&&timeStr.trim().length()==19){
                if(timeStr.contains("/")){
                    time=DateUtils.parseDate(timeStr,"yyyy/MM/dd HH:mm:ss");
                }else if(timeStr.contains("-")){
                    time=DateUtils.parseDate(timeStr,"yyyy-MM-dd HH:mm:ss");
                }
            }else{
                //今日头条每条记录都有CurrentTime时间；
                if(StringUtils.isNotBlank(dataListBean.getCurrentTime())){
                    String currentTime=dataListBean.getCurrentTime().substring(0,19);
                    time=DateUtils.parseDate(currentTime,"yyyy-MM-dd HH:mm:ss");
                }
            }
            //对于没有CurrentTime时间的，就用系统当前时间；
            if(time==null){
                time=new Date();
            }
        } catch (ParseException e) {
            e.printStackTrace();
            time=new Date();
            logger.info("handlerRequestDate is errer");
        }
        return time;
    }
    /**
     * 根据类别得到类别的id
     * @param cmsNewClassInfos
     * @param className
     * @return
     */
    public String getNewClassId(List<NewClassInfo> cmsNewClassInfos, String className){
        if(StringUtils.isBlank(className)){
            return "";
        }
        for (NewClassInfo cmsNewClassInfo:cmsNewClassInfos) {
            if(cmsNewClassInfo.getAlias().equals(className)){
                return cmsNewClassInfo.getClassId();
            }
        }
        return "";
    }
    /**
     * 校验的新闻数据完整性
     * @param exportDataBeans
     * @return
     */
    public List checkExportData(List<BzyNewsDataBean.DataBean.DataListBean> exportDataBeans)
    {
        logger.info("checkExportData start size "+exportDataBeans.size());
        ArrayList<BzyNewsDataBean.DataBean.DataListBean> checkArray=new ArrayList<>();
        for (BzyNewsDataBean.DataBean.DataListBean dataListBean:exportDataBeans) {
            String title=dataListBean.getTitle();
            String pageUrl=dataListBean.getPageUrl();
            String className=dataListBean.getClassName();
            String coverUrl=dataListBean.getCoverUrl();
            String content=dataListBean.getContent();
            String time=dataListBean.getTime();
            String source=MyStringUtils.stringTrim(dataListBean.getSource());
            if(StringUtils.isBlank(title)||StringUtils.isBlank(coverUrl) ||
                    StringUtils.isBlank(content)){
                checkArray.add(dataListBean);
            }
            //环球网的新闻-格式有问题
            if(source.equals("环球网")){
                checkArray.add(dataListBean);
            }
        }
        logger.info("checkExportData checkArray size "+checkArray.size());
        exportDataBeans.removeAll(checkArray);
        logger.info("checkExportData end size "+exportDataBeans.size());
        return exportDataBeans;
    }

    /**
     * 新闻去重
     * @param exportDataBeans
     * @return
     */
    public List repeatExportData(List<BzyNewsDataBean.DataBean.DataListBean> exportDataBeans) {
        logger.info("repeatExportData start size "+exportDataBeans.size());

        ArrayList<BzyNewsDataBean.DataBean.DataListBean> pageUrlArray=new ArrayList<>();
        for (int i=0;i<exportDataBeans.size();i++) {
            BzyNewsDataBean.DataBean.DataListBean dataListBean=exportDataBeans.get(i);
            //数据库是否有重复的
            String pageUrl=dataListBean.getPageUrl();
            String title=dataListBean.getTitle();
            String base64Title=Base64Utils.encodeToString(dataListBean.getTitle().getBytes());
            String coverUrl=dataListBean.getCoverUrl();
            List<NewList> newLists = newListRepository.findByTitleOrCoverUrlAndDelFlag(base64Title,coverUrl,"0");
            if(!CollectionUtils.isEmpty(newLists)&&newLists.size()>0){
                pageUrlArray.add(dataListBean);
                continue;
            }
            //当前列表有重复的
            for (int j=(i+1);j<exportDataBeans.size();j++) {
                BzyNewsDataBean.DataBean.DataListBean dataListBean1=exportDataBeans.get(j);
                //数据库是否有重复的
                String pageUrl1=dataListBean1.getPageUrl();
                String title1=dataListBean1.getTitle();
                String coverUrl1=dataListBean1.getCoverUrl();
                if(title.equals(title1)){
                    pageUrlArray.add(dataListBean1);
                    continue;
                }
            }
        }

        logger.info("repeatExportData pageUrlArray size "+pageUrlArray.size());
        exportDataBeans.removeAll(pageUrlArray);
        logger.info("repeatExportData end size "+exportDataBeans.size());
        return exportDataBeans;
    }

    /**
     * 手动生成数据-模拟八爪鱼的数据
     * @param exportDataBean
     * @return
     */
    public ResultVO generateData(BzyNewsDataBean.DataBean exportDataBean){
        if(null==exportDataBean){
            logger.info("generateData exportDataBean is null ");
        }
        List<BzyNewsDataBean.DataBean.DataListBean> exportDataBeans=exportDataBean.getDataList();
        if(CollectionUtils.isEmpty(exportDataBeans)){
            logger.info("generateData exportDataBeans is null ");
        }
        //3.1 判断数据完整性
        List<BzyNewsDataBean.DataBean.DataListBean> checkDataBeansRepeat=checkExportData(exportDataBeans);
        //4 去重判断；
        List<BzyNewsDataBean.DataBean.DataListBean> exportDataBeansRepeat= repeatExportData(checkDataBeansRepeat);
        //5 正文，列表分开存储；
        saveExportData(exportDataBeansRepeat,"金色财经");
        return ResultVOUtil.success();
    }


}
